package com.my.oschina.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.my.oschina.MainActivity;
import com.my.oschina.R;
import com.my.oschina.adapter.QuestionFragmentPagerAdapter;
import com.my.oschina.common.SimpleTwoTabsListener;

/**
 * 侧边栏导航-问答
 */
public class QuestionFragment extends SherlockFragment {

	private ViewPager viewPager;
	private List<Fragment> mentionFragments = new ArrayList<Fragment>();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initActionBarAndTab(getSherlockActivity().getSupportActionBar(),
				R.string.tab_question_ask, R.string.tab_question_share,
				R.string.tab_question_other, R.string.tab_question_job,
				R.string.tab_question_site, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.viewpager_layout, container,
				false);
		viewPager = (ViewPager) view;
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		viewPager.setOffscreenPageLimit(5);
		viewPager.setOnPageChangeListener(onPageChangeListener);

		QuestionFragmentPagerAdapter adapter = new QuestionFragmentPagerAdapter(
				this, viewPager, getChildFragmentManager(),
				(MainActivity) getSherlockActivity(), mentionFragments);
		viewPager.setAdapter(adapter);
	}

	/**
	 * 初始化actionbar并给viewpager不同页面设置标题
	 * 
	 * @param actionBar
	 * @param firstTab
	 * @param secondTab
	 * @param thirdTab
	 * @param forthTab
	 * @param fifthTab
	 * @param nav
	 */
	public void initActionBarAndTab(ActionBar actionBar, int firstTab,
			int secondTab, int thirdTab, int forthTab, int fifthTab, int nav) {

		actionBar.setLogo(R.drawable.slm_action_help);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.removeAllTabs();

		SimpleTwoTabsListener tabListener = new SimpleTwoTabsListener(viewPager);
		actionBar.addTab(actionBar.newTab().setText(firstTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(secondTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(thirdTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(forthTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(fifthTab)
				.setTabListener(tabListener));

		if (actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS)
			actionBar.setSelectedNavigationItem(nav);
	}

	/**
	 * ViewPager.SimpleOnPageChangeListener
	 */
	ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			if (getSherlockActivity().getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS)
				getSherlockActivity().getSupportActionBar()
						.setSelectedNavigationItem(position);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			super.onPageScrollStateChanged(state);
			// 暂不知具体作用
			switch (state) {
			case ViewPager.SCROLL_STATE_SETTLING:
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 返回问答的问答Fragment-这里使用了嵌套的Fragment
	 * 
	 * @return fragment
	 */
	public QuestionChildFragment getQuesChildFragment(int type) {
		// 由于多个Tab使用同一个QuestionChildFragment，因此这种获取方式应该是不对的
		QuestionChildFragment fragment = ((QuestionChildFragment) getChildFragmentManager()
				.findFragmentByTag(QuestionChildFragment.class.getName()));
		if (fragment == null) {
			fragment = QuestionChildFragment.newInstance(type);
		}
		return fragment;
	}

}
